package admin.controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.CategoryModel;
import models.UserModel;

import entities.Article;
import entities.Category;
import entities.User;

import admin.models.ArticleModel;

public class ArticleSearchServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		String id = "";
		String title = "";
		
		List<Article> articles = new ArrayList<Article>();
		Map<String, Category> categories = new TreeMap<String, Category>();
		Map<String, User> authors = new TreeMap<String, User>();
		
		request.setAttribute("id", id);
		request.setAttribute("title", title);
		request.setAttribute("articles", articles);
		request.setAttribute("categories", categories);
		request.setAttribute("authors", authors);
		try {
			request.getRequestDispatcher("/admin/pages/search_article.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		String id = (String) request.getParameter("id");
		String title = (String) request.getParameter("title");
		
		ArticleModel articleModel = ArticleModel.getInstance();
		List<Article> articles = new ArrayList<Article>();
		if (id != null && !id.isEmpty()) {
			Article article = articleModel.getArticle(id);
			if (article != null) {
				articles.add(article);
			}
		} else if (title != null && !title.isEmpty()) {
			String language = (String) request.getAttribute("language");
			if (language == null || language.isEmpty()) {
				language = "en";
			}
			int page = 1;
			int limit = 20;
			articles = articleModel.searchByName(title, page, limit, language);
		}
		
		//Get categories
		List<String> cIds = new ArrayList<String>();
		List<String> uIds = new ArrayList<String>();
		
		for (int i = 0; i < articles.size(); i++) {
			if (!articles.get(i).getCategory().isEmpty()) {
				cIds.add(articles.get(i).getCategory());
			}
			if (!articles.get(i).getCreator().isEmpty()) {
				uIds.add(articles.get(i).getCreator());
			}
		}
		
		CategoryModel categoryModel = CategoryModel.getInstance();
		Map<String, Category> categories = categoryModel.getByListId(cIds);
		
		UserModel userModel = UserModel.getInstance();
		Map<String, User> authors = userModel.getByListId(uIds);
		
		request.setAttribute("id", id);
		request.setAttribute("title", title);
		request.setAttribute("articles", articles);
		request.setAttribute("categories", categories);
		request.setAttribute("authors", authors);
		try {
			request.getRequestDispatcher("/admin/pages/search_article.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
